﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="EmpAuth.aspx.cs" Inherits="WebForm_Auth_EmpAuth" %>
<script runat="server">
    /* list */
    protected void gv_DataBinding(object sender, EventArgs e)
    {
        var sql = new StringBuilder();
        var dbparams = new List<DBParameter>();

        sql.AppendFormat(string.Format(@"SELECT r.* FROM RoleUserMap r WHERE RoleID IN ({0}) "
            , MyObj.JoinFormat(",", "'{0}'", RoleManager.Current.GetRolesForUser())));
        
        if (!string.IsNullOrEmpty(txtRoleID.Text))
        {
            sql.AppendFormat("AND r.RoleID = @RoleID ");
            dbparams.Add(new DBParameter("RoleID", txtRoleID.Text));
        }

        if (txtEmpID.Text != string.Empty)
        {
            sql.AppendFormat("AND r.EmpID = @EmpID ");
            dbparams.Add(new DBParameter("EmpID", txtEmpID.Text));
        }
                
        var db = new DB();        
        var dt = db.Execute(sql.ToString(), dbparams.ToArray());

        (sender as DataBoundControl).DataSource = dt;

    }

    protected void listRoleID_DataBinding(object sender, EventArgs e)
    {
        (sender as IListControl).DataValueField = "Value";
        (sender as IListControl).DataTextField = "Value";
        (sender as IListControl).DataSource = RoleManager.Current.GetRolesForUser().ToDictionary<string, string>(t => t);
    }
    /* end of list */

    /* form */

    /* end of form */
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <table>
        <tr>
            <td class="label">
                <asp:Localize ID="Localize1" runat="server" Text="<%$Resources: Role, RoleID %>"></asp:Localize>
            </td>
            <td class="text">
                <asp:TextBox ID="txtRoleID" runat="server"></asp:TextBox>
                <asp:Panel ID="pnlRoleID" runat="server" SkinID="Popup" Width="300">
                    <asp:UpdatePanel ID="upRoleID" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <My:ListPopup ID="listRoleID" runat="server" OnDataBinding="listRoleID_DataBinding"
                                Clearable="true"></My:ListPopup>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </asp:Panel>
                <ajax:PopupControlExtender ID="peRoleID" runat="server" TargetControlID="txtRoleID"
                    PopupControlID="pnlRoleID" Position="Bottom">
                </ajax:PopupControlExtender>
            </td>
            <td class="label">
                <asp:Localize ID="Localize2" runat="server" Text="<%$Resources: EmpData, EmpID %>"></asp:Localize>
            </td>
            <td class="text">
                <asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox>
            </td>
            <td class="text">
                <asp:Button ID="btnAdd" runat="server" Text="<%$Resources: Form, btnAdd %>" OnClick="btnAdd_Click" />
                    <ajax:ConfirmButtonExtender ID="cbAdd" runat="server" TargetControlID="btnAdd"
                        ConfirmText="<%$Resources: Form, cb %>">
                    </ajax:ConfirmButtonExtender>
                <asp:Button ID="btnQuery" runat="server" Text="<%$Resources: Form, btnQuery %>" OnClick="btnQuery_Click" />
            </td>
        </tr>
    </table>
    <My:MyGridView ID="gv" runat="server" OnDataBinding="gv_DataBinding" OnRowCommand="gv_ItemCommand"
        DataKeyNames="EmpID, RoleID">
        <Columns>
            <asp:TemplateField HeaderText="<%$Resources: EmpData, EmpID   %>">
                <ItemTemplate>
                    <%# Eval("EmpID") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="<%$Resources: Role, RoleID   %>">
                <ItemTemplate>
                    <%# Eval("RoleID") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="btnDelete" runat="server" CommandName="MyDelete" Text="<%$Resources: Form, btnDelete %>"></asp:LinkButton>
                    <ajax:ConfirmButtonExtender ID="cbDelete" runat="server" TargetControlID="btnDelete"
                        ConfirmText="<%$Resources: Form, cbDelete %>">
                    </ajax:ConfirmButtonExtender>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </My:MyGridView>
</asp:Content>

